package com.example.screenplayer;

import android.media.MediaCodec;
import android.media.MediaFormat;
import android.view.Surface;

import java.io.IOException;
import java.nio.ByteBuffer;

public class ScreenDecoder {

  private static final int VIDEO_WIDTH = 480;
  private static final int VIDEO_HEIGHT = 840;
  private static final int SCREEN_FRAME_RATE = 10;
  private static final long DECODE_TIME_OUT = 10000;
  private static final int SCREEN_FRAME_INTERVAL = 1;
  private MediaCodec mMediaCodec;

  public ScreenDecoder() {}

  public void startDecode(Surface surface) {
    try {
      // 配置MediaCodec
      mMediaCodec = MediaCodec.createDecoderByType(MediaFormat.MIMETYPE_VIDEO_HEVC);
      MediaFormat mediaFormat =
          MediaFormat.createVideoFormat(MediaFormat.MIMETYPE_VIDEO_HEVC, VIDEO_WIDTH, VIDEO_HEIGHT);
      mediaFormat.setInteger(MediaFormat.KEY_BIT_RATE, VIDEO_WIDTH * VIDEO_HEIGHT);
      mediaFormat.setInteger(MediaFormat.KEY_FRAME_RATE, SCREEN_FRAME_RATE);
      mediaFormat.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, SCREEN_FRAME_INTERVAL);
      mMediaCodec.configure(mediaFormat, surface, null, 0);
      mMediaCodec.start();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public void decodeData(byte[] data) {
    // 得到填充了有效数据的input buffer的索引
    int index = mMediaCodec.dequeueInputBuffer(DECODE_TIME_OUT);
    if (index >= 0) {
      // 获取输入缓冲区
      ByteBuffer inputBuffer = mMediaCodec.getInputBuffer(index);
      // 清除原来的内容以接收新的内容
      inputBuffer.clear();
      inputBuffer.put(data, 0, data.length);
      // 将其提交给编解码器 把缓存数据入队
      mMediaCodec.queueInputBuffer(index, 0, data.length, System.currentTimeMillis(), 0);
    }
    MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
    // 请求一个输出缓存
    int outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, DECODE_TIME_OUT);
    // 直到outputBufferIndex < 0 才算处理完所有数据
    while (outputBufferIndex > 0) {
      mMediaCodec.releaseOutputBuffer(outputBufferIndex, true);
      outputBufferIndex = mMediaCodec.dequeueOutputBuffer(bufferInfo, 0);
    }
  }

  public void stopDecode() {
    if (mMediaCodec != null) {
      mMediaCodec.release();
    }
  }
}
